<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\order\Order;
use app\common\model\requirement\Lists;
use app\common\model\requirement\Offer;
use app\common\model\User;

/**
 * 员工接口
 */
class Employee extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (更新最后一次经纬度)
     * @ApiMethod (POST)
     * @ApiParams (name="lng", type="string", required=true, sample="", description="经度")
     * @ApiParams (name="lat", type="string", required=true, sample="", description="纬度")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lnglat()
    {
        $post = request()->post();
        extract($post);
        $user = User::get($this->auth->id);
        if ($lng && $lat) {
            $user->lng = $lng;
            $user->lat = $lat;
            $user->save();
        }
        $this->success('');
    }
    /**
     * @Author LK
     * @ApiTitle (订单列表)
     * @ApiMethod (GET)
     * @ApiParams (name="status", type="integer", required=true, sample="", description="订单状态:10=待服务；20=服务中；30=已完成")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $get = request()->get();
        extract($get);
        $order = new Order;

        if (isset($status) && $status) {
            if ($status == 'all') {
                $order->where('status', '<>', '-1');
            } elseif ($status == '30') {
                $order->whereIn('status', ['30', '40']);
            } else {
                $order->where('status', $status);
            }
        }
        $category = $order->with(['info'])->where('employee_id', $this->auth->id)->order('id desc')->paginate();
        $this->success('请求成功', compact('category'));
    }

    /**
     * @Author LK
     * @ApiTitle (开始服务)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function start_server()
    {
        $post = request()->post();
        extract($post);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $order = Order::get($id);
        if (!$order) {
            $this->error('订单不存在');
        }
        if ($order->status != '10') {
            $this->error('订单状态错误');
        }
        $order->status = '20';
        $order->servertime = time();
        $order->save();
        $this->success('成功');
    }
    /**
     * @Author LK
     * @ApiTitle (广场大厅)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="integer", required=true, sample="", description="发布方式:1=竞价,2=线下沟通")
     * @ApiParams (name="province", type="string", required=true, sample="", description="省")
     * @ApiParams (name="city", type="string", required=true, sample="", description="市")
     * @ApiParams (name="area", type="string", required=true, sample="", description="区")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function plaza_lobby()
    {
        $get = request()->get();
        extract($get);
        $plaza = new Lists();
        if (!isset($type) || !$type) {
            $this->error('参数错误');
        }
        $plaza->where('type', $type);
        if (isset($province) && $province) {
            $plaza->where('province', $province);
        }
        if (isset($city) && $city) {
            $plaza->where('city', $city);
        }
        if (isset($area) && $area) {
            $plaza->where('area', $area);
        }
        $list = $plaza->where('status', '20')->order('id desc')->paginate();
        $this->success('成功', $list);
    }

    /**
     * @Author LK
     * @ApiTitle (竞价)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="需求ID")
     * @ApiParams (name="money", type="string", required=true, sample="", description="出价金额")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function offer()
    {
        $post = request()->post();
        extract($post);
        $res = $this->validate($post, [
            'id' => 'require|gt:0|number',
            'money' => 'require|gt:0|number'
        ]);
        if (true !== $res) {
            $this->error($res);
        }
        $data = [
            'r_id' => $id,
            'user_id' => $this->auth->id,
            'bid_price' => $money,
        ];
        $offer = new Offer($data);
        $check = Offer::get(['r_id' => $id, 'user_id' => $this->auth->id]);
        if ($check) {
            $this->error('已出价');
        }
        $offer->allowField(true)->save();
        $this->success('报价成功');
    }
    /**
     * @Author LK
     * @ApiTitle (购买需求)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="需求ID")
     * @ApiParams (name="oid", type="int", required=true, sample="", description="出价id")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function pay_requirement()
    {
        $uid = $this->auth->id;
        $post = request()->post();
        extract($post);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $order = Lists::get($id);
        if (!$order) {
            $this->error('订单不存在');
        }
        if ($order->status != '20') {
            $this->error('订单状态错误');
        }
        // $order->sn = sn('requirement', 'sn', 'RE');
        if ($order->type == 2) {
            if ($order->employee_id > 0) {
                if ($order->employee_id != $this->auth->id) {
                    $this->error('需求支付中');
                }
            }
            $order->pay_money = config('site.requirement_price');
            $order->employee_id = $uid;
        } else {
            $data = Offer::get($oid);
            if (!$data) {
                $this->error('报价不存在');
            }
            $order->employee_id = $data->user_id;
            $order->pay_money = $data->bid_price;
        }
        $order->pay_uid = $uid;
        $order->allowField(true)->save();
        if ($order->type == 2) {
            queue('app\common\job\Requirement', $order->id, 60, 'offer1246');
        }
        $this->success('提交成功', $order->id);
    }
    /**
     * @Author LK
     * @ApiTitle (员工广场订单)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="integer", required=true, sample="", description="1=已出价；2=待服务；3=已完成 ")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function  plaza_order()
    {
        $get = request()->get();
        extract($get);
        $uid = $this->auth->id;
        $list = new Lists();
        if ($type == 1) {
            $r_ids = Offer::whereIn('user_id', $uid)->column('r_id');
            $list->whereIn('id', $r_ids)->where('status', '20')->where(function ($q) {
                // $q->whereOr('employee_id', $this->auth->id);
            });
        } elseif ($type == 2) {
            $list->whereIn('status', ['30', '40'])->where('employee_id', $uid);
        } else {
            $list->where('status', '50')->where('employee_id', $uid);
        }
        $list = $list->order('id desc')->paginate();
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (删除出价广场订单)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="需求ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function del_order()
    {
        $post = request()->post();
        extract($post);
        $list = Offer::get(['r_id' => $id, 'user_id' => $this->auth->id]);
        $list->delete();
        $this->success('', $list);
    }
}
